export default {
  tree_fp_sale_order: {
    _odoo_model: 'ir.ui.view',
    model: 'fp.sale.order',
    type: 'tree',
    priority: 2,

    toolbar: {
      action: [
        {
          name: '创建发票',
          id: 'fp_sale.action_sale_to_fapiao',
          xml_id: 'fp_sale.action_sale_to_fapiao'
        }
      ]
    },

    fields: {
      partner_state: {
        selection: [
          ['draft', '待处理'],
          ['done', '已完成']
          // ['error', '错误']
        ]
      },

      state: {
        selection: [
          ['draft', '草稿'],
          ['confirmed', '待提交'],
          ['done', '已提交'],
          ['cancel', '已作废']
        ]
      },

      company_id: { string: '公司' },
      user_id: { string: '用户' },
      partner_id: { string: '客户' },
      fp_partner_id: { string: '开票客户' },

      invoice_type: {
        selection: [
          ['vat_special', '增值税专用发票'],
          ['vat_general', '增值税普通发票'],
          ['vat_general_e', '增值税电子普通发票'],
          ['vat_special_e', '增值税电子专用发票']
        ]
      },
      client_order_ref: { string: '业务单据号' },

      name: { string: '订单号' },
      date_order: { string: '日期' },
      amount_total: { string: '金额' },

      line_ids: {
        widget: 'x2many_tree',
        // col  * 2
        views: {
          tree: {
            fields: {
              // name: {},
              // sequence: {},
              product_state: {
                selection: [
                  ['draft', '待处理'],
                  ['done', '已完成']
                  // ['error', '错误']
                ]
              },

              order_id: {},
              vat_product_id: { string: '税务商品' },
              product_id: { string: '商品' },
              product_spec: { string: '规格' },
              product_uom: { string: '单位' },
              tax_ratio: { string: '税率' },
              quantity: { string: '数量' },
              price_unit: { string: '含税单价' },

              amount_untaxed: { string: '无税金额' },
              amount_tax: { string: '税额' },
              amount_total: { string: '含税金额' },
              note: { string: '备注' }
            }
          }
        }
      }
    }
  },

  form_fp_sale_order: {
    _odoo_model: 'ir.ui.view',
    model: 'fp.sale.order',
    type: 'form',
    toolbar: {
      action: {},
      print: {}
    },

    arch: {
      header: {
        buttons: [
          {
            name: 'action_confirm',
            string: '确认',
            type: 'object',
            btn_type: 'primary',
            invisible: ({ record }) => {
              const { state } = record
              return state !== 'draft'
            }
          },

          {
            name: 'action_cancel',
            string: '取消',
            type: 'object',
            invisible: ({ record }) => {
              const { id: res_id, state } = record
              return !['draft', 'confirmed', 'done'].includes(state) || !res_id
            }
          },

          {
            name: 'action_draft',
            string: '重置为草稿',
            type: 'object',
            // states: 'cancel',
            invisible: ({ record }) => {
              const { state } = record
              return state !== 'cancel'
            }
          }
        ],

        fields: {
          state: {
            widget: 'statusbar',
            statusbar_visible: 'draft,confirmed,done'
          }
        }
      }
    },

    fields: {
      invoice_type: {},
      // state: { invisible: 1 },
      state: {
        string: '状态'
        // selection: [['draft', '草稿']]
      },
      client_order_ref: { string: 'NC发票PK' },
      name: { string: '订单号' },
      partner_id: { domain: [], string: '客户' },

      date_order: { string: '订单日期' },
      amount_total: { string: '金额' },

      line_ids: {
        widget: 'x2many_tree',
        // col  * 2
        views: {
          tree: {
            fields: {
              // name: {},
              // sequence: {},
              order_id: {},
              // product_categ_id: { string: '开票商品类别' },
              product_id: { string: '商品' },
              product_spec: { string: '规格' },
              product_uom: { string: '单位' },
              tax_ratio: { string: '税率' },
              quantity: { string: '数量' },
              price_unit: { string: '含税单价' },

              amount_untaxed: { string: '不含税金额' },
              amount_tax: { string: '税' },
              note: { string: '备注' }
            }
          },

          form: {
            fields: {
              name: {},
              sequence: {},
              // product_categ_id: {},
              product_id: { domain: [] },
              product_spec: {},
              product_uom: {},

              tax_ratio: {},
              quantity: {},
              price_unit: {},
              amount_discount: {},
              amount_untaxed: {},
              amount_tax: {},
              note: {}
            }
          }
        }
      }
    }
  },

  search_fp_sale_order: {
    _odoo_model: 'ir.ui.view',
    model: 'fp.sale.order',
    type: 'search',
    arch: {
      fields: {
        // name: {
        //   string: '订单',
        //   filter_domain: self => {
        //     return [
        //       '|',
        //       '|',
        //       ['name', 'ilike', self],
        //       ['client_order_ref', 'ilike', self],
        //       ['partner_id', 'child_of', self]
        //     ]
        //   }
        // },
        partner_id: { operator: 'child_of' }
        // user_id: {},
        // team_id: {},
        // order_line: {
        //   string: '产品',
        //   filter_domain: self => {
        //     return [['order_line.product_id', 'ilike', self]]
        //   }
        // },
        // analytic_account_id: {}
      },

      filters: {
        // group1: {
        //   my_sale_orders_filter: {
        //     string: '我的订单',
        //     domain: ({ env }) => {
        //       const uid = env.uid
        //       return [['user_id', '=', uid]]
        //     }
        //   }
        // },
        group1: {
          state_draft: {
            string: '草稿',
            domain: [['state', 'in', ['draft', 'confirmed']]]
          },
          state_done: {
            string: '已完成',
            domain: [['state', 'in', ['done']]]
          }
        },
        group3: {
          filter_create_date: { string: '创建日期', date: 'create_date' }
        },

        group4: {
          vat_general: {
            string: '增值税普通发票',
            domain: [['invoice_type', 'in', ['vat_general']]]
          },
          vat_special: {
            string: '增值税专用发票',
            domain: [['invoice_type', 'in', ['vat_special']]]
          },
          vat_general_e: {
            string: '增值税电子普通发票',
            domain: [['invoice_type', 'in', ['vat_general_e']]]
          },
          vat_special_e: {
            string: '增值税电子专用发票',
            domain: [['invoice_type', 'in', ['vat_special_e']]]
          }
        }
      }
    }
  },

  action_fp_sale_order: {
    _odoo_model: 'ir.actions',
    name: '销售订单',
    type: 'ir.actions.act_window',
    res_model: 'fp.sale.order',
    search_view_id: 'search_fp_sale_order',
    domain: [],
    context: {},
    views: {
      tree: 'tree_fp_sale_order',
      form: 'form_fp_sale_order'
    }
  }
}
